package com.shop.cereshop.app.tencent.moderation.service;

import cn.hutool.core.collection.CollUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.tencent.moderation.constant.ModerationConstant;
import com.shop.cereshop.commons.tencent.moderation.domain.request.CreateVideoModerationTaskParam;
import com.shop.cereshop.commons.tencent.moderation.domain.request.ImageModerationParam;
import com.shop.cereshop.commons.tencent.moderation.domain.request.TextModerationParam;
import com.shop.cereshop.commons.tencent.moderation.domain.response.ModerationResult;
import com.shop.cereshop.commons.tencent.moderation.domain.response.VideoModerationResult;
import com.shop.cereshop.commons.tencent.moderation.service.ImageModerationService;
import com.shop.cereshop.commons.tencent.moderation.service.TextModerationService;
import com.shop.cereshop.commons.tencent.moderation.service.VideoModerationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 内容审核
 * @author zdd
 * @date 2025/10/29 19:20
 * @version 1.0
 */
@Slf4j
@Service
public class ContentCheckService {

    @Resource
    private TextModerationService textModerationService;
    @Resource
    private ImageModerationService imageModerationService;
    @Resource
    private VideoModerationService videoModerationService;


    /**
     * 文本内容审核
     * @param content 评论内容
     * return 结果
     */
    public Result<Object> checkTextContent(String content) {
        if (StringUtils.isBlank(content)) {
            log.warn("评论内容为空");
            return Result.success();
        }
        TextModerationParam textParam = new TextModerationParam();
        textParam.setContent(content);
        ModerationResult result = textModerationService.moderateText(textParam);
        if (result != null && !ModerationConstant.SUGGESTION_PASS.equals(result.getSuggestion())) {
            return Result.failed(CoReturnFormat.FAIL, result.getMessage());
        }
        return Result.success();
    }

    /**
     * 单张图片审核
     * @param imageUrl 图片地址
     * return 结果
     */
    public Result<Object> checkSingleImage(String imageUrl) {
        if (StringUtils.isBlank(imageUrl)) {
            log.warn("图片地址为空");
            return Result.success();
        }
        ImageModerationParam imageParam = new ImageModerationParam();
        imageParam.setImageUrl(imageUrl);
        ModerationResult result = imageModerationService.moderateImage(imageParam);
        if (result != null && !ModerationConstant.SUGGESTION_PASS.equals(result.getSuggestion())) {
            return Result.failed(CoReturnFormat.FAIL, result.getMessage());
        }
        return Result.success();
    }

    /**
     * 多张图片审核
     * @param imageUrls 图片地址列表
     * return 结果
     */
    public Result<Object> checkBatchImages(List<String> imageUrls) {
        if (CollUtil.isEmpty(imageUrls)) {
            log.warn("图片地址列表为空");
            return Result.success();
        }

        try {
            List<ImageModerationParam> params = new ArrayList<>();
            for (String url : imageUrls) {
                if (StringUtils.isBlank(url)) {
                    continue;
                }
                ImageModerationParam param = new ImageModerationParam();
                param.setImageUrl(url);
                params.add(param);
            }

            if (params.isEmpty()) {
                return Result.success();
            }

            List<ModerationResult> results = imageModerationService.batchModerateImages(params);
            if (results != null) {
                for (ModerationResult result : results) {
                    if (result != null && !ModerationConstant.SUGGESTION_PASS.equals(result.getSuggestion())) {
                        return Result.failed(CoReturnFormat.FAIL, result.getMessage());
                    }
                }
            }
        } catch (Exception e) {
            log.error("图片审核服务异常", e);
            return Result.failed("图片审核服务暂时不可用，请稍后再试");
        }

        return Result.success();
    }

    /**
     * 创建视频审核任务
     * @param videoUrl 视频地址
     * return 结果
     */
    public Result<Object> createModerationTask(String videoUrl, String taskType, String storageType, Long priority) {
        if (StringUtils.isBlank(videoUrl)) {
            log.warn("视频地址为空");
            return Result.success();
        }
        CreateVideoModerationTaskParam createTaskParam = new CreateVideoModerationTaskParam();
        createTaskParam.setVideoUrl(videoUrl);
        createTaskParam.setTaskType(taskType);
        createTaskParam.setStorageType(storageType);
        createTaskParam.setPriority(priority);
        VideoModerationResult createTaskResult = videoModerationService.createModerationTask(createTaskParam);

        if (createTaskResult != null && !ModerationConstant.TASK_STATUS_PENDING.equals(createTaskResult.getStatus())) {
            return Result.failed(CoReturnFormat.FAIL, createTaskResult.getMessage());
        }
        return Result.success();
    }


}
